package vrp;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import java.util.Date;

import java.io.IOException;
import javax.jdo.PersistenceManager;
import javax.servlet.http.*;

import vrp.Product;
import vrp.PMF;

public class ProductEditServlet extends HttpServlet {

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
    	
    	PersistenceManager pm = PMF.get().getPersistenceManager();
    	
        long keyid = Integer.parseInt(req.getParameter("key"));
        String name = req.getParameter("name");
        double price = Float.parseFloat(req.getParameter("price"));
        double weight = Float.parseFloat(req.getParameter("weight"));
        int inventory = Integer.parseInt(req.getParameter("inventory"));

        try {
            Key key = KeyFactory.createKey(Product.class.getSimpleName(), keyid);
            Product product = pm.getObjectById(Product.class, key);
            
            product.setName(name);
            product.setPrice(price);
            product.setWeight(weight);
            product.setInventory(inventory);
            product.setModifiedAt(new Date());
            pm.makePersistent(product);
        } finally {
            pm.close();
        }
        resp.sendRedirect("/products.jsp");
    }
}